#try:
#  from xml.etree import ElementTree
#except ImportError:
#  from elementtree import ElementTree
#import gdata.spreadsheet.service
#import gdata.service
#import atom.service
#import gdata.spreadsheet
#import atom
import getopt
import sys
import string

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import datetime
import cgi
from google.appengine.ext import db

#START: ShowConsentPage
class ShowConsentPage(webapp.RequestHandler):
    def get(self):
        self.response.headers["Content-Type"] = "text/html"
        self.response.out.write("""
           <html>
           <head><title>SYSL's AppEngine Consent Proof Page (last 20)</title>
           </head>
           <body>
           <p>(Current time is %s; viewing the last 20 messages.)</p>
           """ % (datetime.datetime.now()))
        # Output the set of consent info messages
        messages = db.GqlQuery('SELECT * From RecordConsent')
        for user in messages:
            self.response.out.write("<p>%s</p>" % user)
        self.response.out.write("""
         <a href="/Home.html">Home Page</a></p>
         </body>
         </html>""")
#END: ShowConcentPage

#START: RecordConsent
class RecordConsent(db.Model):
    user = db.StringProperty(required=True)
    timestamp = db.DateTimeProperty(auto_now_add=True)
    consentstamp = db.TextProperty(required=False)

    def __str__(self):
        return "%s (%s): %s" % (self.user, self.timestamp, self.consentstamp)
#END: RecordConsent

# START: ConsentPostHandler
class ConsentPoster(webapp.RequestHandler):
    def post(self):
        fname = self.request.get("txtFirstName")
        lname = self.request.get("txtLastName")
        DOB = self.request.get("txtDOB")
        EMail = self.request.get("txtEmailAdd")
        chkbox = self.request.get("chkbxConsent")
        fnlname = fname + lname + DOB + EMail
        msg = RecordConsent(user=fnlname, consentstamp=chkbox)
        msg.put()
        self.redirect('/GetStarted.html')        
# END: ConsentPostHandler

# Create Three Classes to show three different html pages
class HomePage(webapp.RequestHandler):
    def get(self):
		self.redirect("/Home.html")
           
class GetStartedPage(webapp.RequestHandler):
	def get(self):
		self.redirect("/GetStarted.html")
		
class RetrieveResultsPage(webapp.RequestHandler):
	def get(self):
		self.redirect("/RetrieveResults.html")
                            
application = webapp.WSGIApplication([('/', HomePage),
                                      ('/secondpage', GetStartedPage),
                                      ('/thirdpage', RetrieveResultsPage),
                                      ('/firstfrm', ConsentPoster),
                                      ('/shconsent', ShowConsentPage),                                                                            
                                      ],
                                     debug=True)
  
  
def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()
    